#!/bin/bash

cd /
image=$1

# check if directory exists
if [ ! -d $image ]
then
 echo "The directory $patchdir doesn't exist"
 echo "Please specify directory containing the patch files."
 exit 1
fi

image=$1

rpm -U -vv $image/IBMhsc.coreserver-1.1-2.i386.rpm --force --nodeps
rpm -U -vv $image/IBMhsc.cimprovider-1.1-2.i386.rpm --force --nodeps
rpm -U -vv $image/IBMhsc.bundles_en_US-1.1-2.i386.rpm --force --nodeps
rpm -U -vv $image/IBMhsc.pdml_en_US-1.1-2.i386.rpm --force --nodeps
rpm -U -vv $image/IBMhsc.plfmgt-1.1-2.i386.rpm --force --nodeps
rpm -U -vv $image/IBMhsc.lparmgt-1.1-2.i386.rpm --force --nodeps

rpm -U -vv $image/rsct.core-2.2.0.23-0.i386.rpm --force --nodeps
rpm -U -vv $image/rsct.service-1.2.0.4-1.i386.rpm --force --nodeps

# Now check to see if this is an HMC serviced by IBM by
# looking to see if SvcAgent is installed and if the
# /opt/hsc/data/ibmserviced.dat file exists

rpm -q -a | grep SvcAgentHSC 2>/dev/null
if [ $? -ne 0 ]
then
  if [ ! -f /opt/hsc/data/ibmserviced.dat ]
  then
     rm -f /usr/websm/config/factory_defaults/AppPD.db
  fi
else
  # attempt an upgrade
  rm -f /tmp/sagent.upgrade 2>/dev/null
  rpm -i $image/SvcAgentHSC-1.0.0-1.i386.rpm --force --nodeps
  if [ $? -ne 0 ]
  then
   if [ -f /usr/svcagent/bin/chkconf ]
   then
	# Erase and re-install only if SA return 11
	/usr/svcagent/bin/chkconf
	if [ $? -eq 11 ]
	then
	   rpm -e SvcAgentHSC
  	   rpm -i $image/SvcAgentHSC-1.0.0-1.i386.rpm --force --nodeps
	fi
   else
      # should not get here because chkconf should always be around
      rpm -e SvcAgentHSC
      rpm -i $image/SvcAgentHSC-1.0.0-1.i386.rpm --force --nodeps
   fi
 fi
fi
exit 0
